﻿using S7SingalTool.Models.Table;
using S7SingalTool.Models.ViewModels;
using S7SingalTool.Utils;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace S7SingalTool.Forms
{
    public partial class BlockDbEditForm : BaseForm
    {
        public BlockDbEditForm()
        {
            InitializeComponent();
            this.Load += BlockDbEditForm_Load;
        }

        private void BlockDbEditForm_Load(object sender, EventArgs e)
        {
            InitData();
        }
        
        private void InitData()
        {
          
            var lst = LocalApp.fsql.Queryable<PlcConfig>().ToList();
            List<SelectOption> plcItems = new List<SelectOption>();
            foreach (var item in lst)
            {
                plcItems.Add(new SelectOption() { Text = item.WorkstationCode,Source = item.Id });
            }
            selectPlc.Items.AddRange(plcItems.ToArray());

            //写入方:1 PC、2 PLC
           // selectWriteFrom.Items.Add(new SelectOption() { Text ="PC",Source = 1 });
            //selectWriteFrom.Items.Add(new SelectOption() { Text = "PLC", Source = 2 });
            //selectWriteFrom.Items.Add(new SelectOption() { Text = "混合", Source = 3 });
            //
            selectPlc.MouseLeave += SelectPlc_MouseLeave;
          

        }

        private void SelectWriteFrom_MouseLeave(object sender, EventArgs e)
        {
            
        }

        private void SelectPlc_MouseLeave(object sender, EventArgs e)
        {
            var item = selectPlc.SelectedValue;
            if (item != null)
            {
                selectPlc.Text = (item as SelectOption).Text;
            }
        }

       

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var item1 = selectPlc.SelectedValue;
          
            int blockDb = txtBlockDb.Text.ObjToInt();
           
            if (item1 is null )
            {
                AntdUI.Message.error(this, "请填写必填项", autoClose: 2);
                return;
            }
            if(blockDb <1)
            {
                AntdUI.Message.error(this, "请填写Db块编号", autoClose: 2);
                return;
            }
            PlcBlockDb plcBlockDb = new PlcBlockDb()
            {
                WriteFrom = 3,
                PlcId = (item1 as SelectOption).Source.ObjToInt(),
                BlockDb = blockDb,
                Remark = txtRemark.Text,
                Module = 1,
            };
            LocalApp.fsql. Insert(plcBlockDb).ExecuteAffrows();

            DialogResult  = DialogResult.OK;
        }
    }
}
